<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">

    string path;
    long ticks;
    protected void Page_Load(object sender, EventArgs e)
    {
        int pid = Convert.ToInt32(Request.QueryString["pid"].ToString());
        if (Page.IsPostBack)
        {
            string fileName, sid;
            HttpPostedFile uploadedFile = file1.PostedFile;            
            
            path = ConfigurationSettings.AppSettings["CMS_Path"].ToString();
            sid = Session.SessionID;
            if (pid == 0)
            {
                path += @"\Temp\" + sid + @"\";
            }
            else
            {
                path += @"\project_files\" + pid + @"\";
            }
            if (uploadedFile != null && uploadedFile.ContentLength > 0)
            {
                fileName = UploadFile(uploadedFile);
                
                //add record
                AddRecord(pid, fileName, ticks, sid);
                //reset status
                string strScript = "<script language=\"javascript\">";
                strScript += "parent.reset();";                
                strScript += "parent.addFile(\"" + Server.HtmlEncode(fileName) + "\", '" + ticks + "');<";
                strScript += "/";
                strScript += "script>";
                this.RegisterClientScriptBlock("reset", strScript);
            }
        }
        
    }
    
    // add record function
    protected void AddRecord(int pid, string filename, long ticks, string sid)
    {
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            SqlCommand cmd = new SqlCommand("sp_AddFile", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@filename", SqlDbType.NVarChar, 120).Value = filename;
            cmd.Parameters.Add("@ticks", SqlDbType.BigInt).Value = ticks;
            cmd.Parameters.Add("@sid", SqlDbType.VarChar, 64).Value = sid;
            cmd.Parameters.Add("@pid", SqlDbType.Int).Value = pid;
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

    // UploadFile function
    protected string UploadFile(HttpPostedFile file)
    {
        string fileName = null;
        int lastPos = file.FileName.LastIndexOf('\\');

        fileName = file.FileName.Substring(++lastPos);

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        ticks = DateTime.Now.Ticks;
        file.SaveAs(path + ticks + "_" + fileName);
        return fileName;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Upload file</title>
    <style type="text/css">
        body { margin:0;}
        input {
            font-family:Tahoma;
            font-size:8pt;
        }
    </style>
    <script language="javascript">
        function upload()
        {
            var frm = document.frmUpload;
            
            if(frm.file1.value.length==0)
            {
                alert("Please choose a file before clicking Upload button.");
                return false;
            }
            parent.hideCtrl();
            parent.showStatus();
            frmUpload.submit();
        }
    </script>
</head>
<body>
    <form id="frmUpload" runat="server">
        <div>
            <input type="file" size="38" id="file1" runat="server" /> &nbsp;
            <input type="button" name="cmdSubmit" value="Upload" onClick="upload();" />
        </div>
    </form>
</body>
</html>
